#-*- coding: utf-8 -*-
from datetime import datetime, timedelta

from apiManager.models import *
import datetime as DT
from eve_static.models import Invtypes, Stastations, Invnames, Invflags


# from pyTree import Tree
# @register.filter
# def get_item(dictionary, key):
#     return dictionary.get(key)
def locate_asset(locationID):
    locInfos = list()
    prefix = str(locationID)[0:2]
    if prefix == 60:
        locInfos.insert(0, locationID+6000001) 
    elif prefix == 66:
        locInfos.insert(0, locationID-6000001)
    elif prefix == 67:
        locInfos.insert(0, locationID-6000000)
    else:
        locInfos.insert(0, locationID)    
    try:
        station = Stastations.objects.get(stationid = locInfos[0])
        locInfos.insert(1, station.stationname)
    except Stastations.DoesNotExist:
        locInfos.insert(1, None)
    try:
        locInfos.insert(2, Invnames.objects.get(itemID = locInfos[0]).itemName)
    except Invnames.DoesNotExist:
        locInfos.insert(2, None) 
    
    return locInfos

def retrieve_invtypes(typeID):
    #récupère l'objet invtypes pour avoir le nom de l'item
    try:
        assetType = Invtypes.objects.get(typeid = typeID)
    except Invtypes.DoesNotExist:
        print "No type !"
        assetType = None

    return assetType

def retrieve_invflags(flagID):
    try:
        invflag = Invflags.objects.get(flagID = flagID).flagText
    except Invflags.DoesNotExist:
        invflag = None
        
    return invflag


def asset_details(assetList):
#     assetType = Invtypes.objects.get(typeid = asset.typeID)
#     assetName = assetType.typename
    start = DT.datetime.utcnow().replace(tzinfo=utc)
    assetDict={}
    parentDict={}
    for asset in assetList:
        #repère pour le nombre de passe de la boucle
        i = 0
        #var déclaration
        assetAttributes = {}
        childsDict = {}
        childList = []
        primary_asset =[]
        #===============================================================
        # arbre parent - enfants
        #===============================================================
        if asset.parentID is None:
            assetAttributes['parent'] =  "No parent"
            #parentDict[asset] = assetAttributes
           
                           
        else:
            assetAttributes['parent'] =  asset.parentID

        
        for subasset in assetList:
                if subasset.parentID == asset.itemID:
                    childList.append(subasset.itemID) 
                    
                else:
                    pass
        
        
        #récupère l'objet invtypes pour avoir le nom de l'item
        assetType = retrieve_invtypes(asset.typeID)
     
        #assignation des valeurs de invtypes        
        assetAttributes['typeID'] = assetType.typeid
        assetAttributes['typeName'] = assetType.typename
        assetAttributes['qty'] = asset.quantity
    
        assetAttributes['flagText'] = retrieve_invflags(asset.flag)   
            
        #==================================================================
        # item location attributions
        #==================================================================
        if asset.locationID is not None:
            locInfos = locate_asset(asset.locationID)
            assetAttributes['locID'] = locInfos[0]
            assetAttributes['location'] = locInfos[1]
            assetAttributes['locationName'] = locInfos[2]
        else:
            assetAttributes['locID'] = None
            assetAttributes['location'] = "no location"
            assetAttributes['locationName'] = "no name"
        
                
        assetAttributes['childs'] = childList
        assetDict[asset] = assetAttributes
        i+=1
        
    end = DT.datetime.utcnow().replace(tzinfo=utc)
    delta = end - start
    delta = delta.total_seconds()
    assetDict['delta'] = delta
    
    return assetDict

# def order_asset(assetDict):
#     ordered_assetDict = {}
#     
#     #===========================================================================
#     # utiliser iter()
#     #===========================================================================
#     
#     
#     control = True
#     while control:
        
#     for assetID, assetAttributes in assetDict.items():
#         parentDict = {}
#         if assetAttributes['childs'] == []:
#             print "no childs = feuilles"
#             parentDict[assetAttributes['parent']] = assetAttributes
#             
#         else:
#             print "asset as childs"
#             pass
        
# def asset_decrypt(assetDict):
#     for key, value in assetDict.items():
        